package com.yejiarong.auth.common.interceptor;

import jakarta.servlet.*;
import jakarta.servlet.http.HttpServletRequest;

import java.io.IOException;

/**
 * @author YeJR
 */
public class RequestWrapperFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        // 仅对 HttpServletRequest 进行包装
        if (servletRequest instanceof HttpServletRequest) {
            CachedRequestWrapper wrappedRequest = new CachedRequestWrapper((HttpServletRequest) servletRequest);
            // 将包装后的请求传递给下一个过滤器
            filterChain.doFilter(wrappedRequest, servletResponse);
        } else {
            // 对非 HttpServletRequest 请求不做任何处理
            filterChain.doFilter(servletRequest, servletResponse);
        }
    }
}
